package com.example.javapracticaltraining.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javapracticaltraining.entity.R;
import com.example.javapracticaltraining.entity.pojo.LeaveApplications;
import com.example.javapracticaltraining.service.LeaveApplicationsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "请假申请管理", description = "请假申请的增删改查接口")
@RestController
@RequestMapping("/leaveApplications")
public class LeaveApplicationsController {

    @Autowired
    private LeaveApplicationsService leaveApplicationsService;

    @Operation(summary = "提交请假申请", description = "创建新的请假申请记录")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "请假申请提交成功")
    })
    @PostMapping
    public R<String> applyForLeave(@RequestBody LeaveApplications leaveApplication) {
        leaveApplication.setStatus("待审批"); // 默认状态为待审批
        leaveApplicationsService.save(leaveApplication);
        return R.success("请假申请提交成功");
    }

    @Operation(summary = "分页查询请假申请", description = "根据条件分页查询请假申请记录")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "查询成功")
    })
    @GetMapping("/page")
    public R<Page<LeaveApplications>> getLeaveApplicationsByPage(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页记录数", example = "10") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "用户ID") Integer userId,
            @Parameter(description = "申请状态，如：待审批、已批准、已拒绝") String status) {

        // 创建分页对象，设置当前页和每页大小
        Page<LeaveApplications> pageInfo = new Page<>();
        pageInfo.setCurrent(page);
        pageInfo.setSize(pageSize);

        // 创建查询条件
        LambdaQueryWrapper<LeaveApplications> queryWrapper = new LambdaQueryWrapper<>();

        if (userId != null) {
            queryWrapper.eq(LeaveApplications::getUserId, userId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(LeaveApplications::getStatus, status);
        }

        // 执行分页查询
        leaveApplicationsService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @Operation(summary = "批准请假申请", description = "管理员批准请假申请")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "请假申请审批成功"),
        @ApiResponse(responseCode = "500", description = "请假申请审批失败")
    })
    @PutMapping("/approve")
    public R<String> approveLeaveApplication(@RequestBody LeaveApplications leaveApplication) {
        // 假设只有管理员或特定角色可以审批
        leaveApplication.setStatus("已批准");
        leaveApplication.setApprovalTime(java.time.LocalDateTime.now());
        boolean success = leaveApplicationsService.updateById(leaveApplication);
        if (success) {
            return R.success("请假申请审批成功");
        } else {
            return R.error("请假申请审批失败");
        }
    }

    @Operation(summary = "拒绝请假申请", description = "管理员拒绝请假申请")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "请假申请已拒绝"),
        @ApiResponse(responseCode = "500", description = "请假申请拒绝失败")
    })
    @PutMapping("/reject")
    public R<String> rejectLeaveApplication(@RequestBody LeaveApplications leaveApplication) {
        // 假设只有管理员或特定角色可以审批
        leaveApplication.setStatus("已拒绝");
        leaveApplication.setApprovalTime(java.time.LocalDateTime.now());
        boolean success = leaveApplicationsService.updateById(leaveApplication);
        if (success) {
            return R.success("请假申请已拒绝");
        } else {
            return R.error("请假申请拒绝失败");
        }
    }

    @Operation(summary = "删除请假申请", description = "根据ID删除请假申请记录")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "请假申请删除成功")
    })
    @DeleteMapping("/{leaveId}")
    public R<String> deleteLeaveApplication(
            @Parameter(description = "请假申请ID", required = true) @PathVariable Integer leaveId) {
        leaveApplicationsService.removeById(leaveId);
        return R.success("请假申请删除成功");
    }
}
